package linechart;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;

//管理具体的曲线型--AILineChart--的Manager，里面有个ID-AILineChart键值对map，有利于添加多条AI曲线
public class AIChartManager {
	// 针对一个图里面可能有多条曲线而设计
	private Map<String, Map<String, AILineChart>> lineChartMap;
	private LineChartCreator lcc;

	public AIChartManager() {
		lineChartMap = new HashMap<String, Map<String, AILineChart>>();
		lcc=new LineChartCreator();
	}

	public boolean isMonitor(String graphid, String devid) {
		if (!lineChartMap.isEmpty()) {
			return lineChartMap.get(graphid).containsKey(devid);
		} else {
			return false;
		}
	}

	public Map<String, AILineChart> getAILineChart(String graphid) {
		return lineChartMap.get(graphid);
	}

	public void createAILineChart(String devid) {

	}

	public AILineChart createAILineChart(String graphid, RealAIInfo realAIInfo) {
		return createAILineChart(graphid, realAIInfo, null);
	}

	public String getChart(String graphid) {
		Map<String, AILineChart> ail = lineChartMap.get(graphid);
		/*for (Object o : ail.keySet()) {	
			for(int i=0;i<lcc.getElements().size(),i++){
			if(!lcc.getElements().contains(o))
			lcc.addElement((String) o, ail.get(o).getName());
		}*/
	//}
		return lcc.getChart();
	}

	public AILineChart createAILineChart(String graphid, RealAIInfo realAIInfo,
			String color) {
		AILineChart aiLineChart = new AILineChart();
		aiLineChart.setRealAIInfo(realAIInfo, color);
		if (!lineChartMap.containsKey(graphid)) {
			Map<String, AILineChart> ail = new HashMap<String, AILineChart>();
			ail.put(realAIInfo.getDevId(), aiLineChart);
			lineChartMap.put(graphid, ail);
		} else {
			if (!lineChartMap.get(graphid).containsKey(realAIInfo.getDevId())) {
				lineChartMap.get(graphid).put(realAIInfo.getDevId(),
						aiLineChart);
			} else {
				lineChartMap.get(graphid).get(realAIInfo.getDevId()).addValue(
						realAIInfo.getDevId());
			}
		}
		return aiLineChart;
	}

	public static AIChartManager getSessionAIChartManager(HttpSession session) {
		final String attrName = "CHART_MANAGER";
		AIChartManager result = null;
		Object o = session.getAttribute(attrName);
		if (o != null) {
			result = (AIChartManager) o;
		} else {
			result = new AIChartManager();
			session.setAttribute(attrName, result);
		}
		return result;
	}
}